<template>
	<om-formsub-outer :index="index" :checkedit="checkedit">
		<om-autoform ref="autoform" :edit="edit" :checkedit="checkedit" :initConfigData="autoformdata" :initValue="initValue"></om-autoform>
	</om-formsub-outer>
</template>

<script>
	import {
		buildSubFormData,
		subOnLoad,
		changeEditor,
		configSelectData
	} from '../../../static/js/formUtil.js';
	import {
		ignoreHtml,
		ptFloat,
		minusF,
		getDateStr
	} from '@/static/js/common.js';
	import urlUtil from '@/static/js/urlUtil.js';
	export default {
		provide() {
			return {
				formPage: this
			};
		},
		data() {
			return buildSubFormData({
				exist:[],
				customerUserCacheKey:'bro-jy-borrowedSample-productList-customer-cache',
				productUserCacheKey:'bro-jy-borrowedSample-productList-productUser-cache'
			})
		},
		methods:{
			onInputContractReduceRate(value){
				let _that = this
				let formdata = _that.$refs.autoform.formdata
				if(formdata.productMoneyOne && formdata.productNumber){
					formdata.productMoneyAll = Number(formdata.productMoneyOne) * Number(formdata.productNumber) * Number(value)
				}
			},
			onInputProductNumber(value){
				let _that = this
				let formdata = _that.$refs.autoform.formdata
				if(formdata.productMoneyOne && formdata.contractReduceRate){
					formdata.productMoneyAll = Number(formdata.productMoneyOne) * Number(formdata.contractReduceRate) * Number(value)
				}
			},
			onSelectTaxRate(value){
				let _that = this
				if(_that.$refs.autoform.formdata.adjustedDiscount){
				    _that.$refs.autoform.formdata.salesGuidancePriceIncludingTax = (Number(_that.$refs.autoform.formdata.productMoneyOne) * (Number(value) / 100 + 1)).toFixed(4)
				}
			},
			onInputProductMoneyOne(value){
				let _that = this
				let formdata = _that.$refs.autoform.formdata
				 if(formdata.taxRate){
				    _that.$refs.autoform.formdata.salesGuidancePriceIncludingTax = (Number(value) * (Number(formdata.taxRate) / 100 + 1)).toFixed(4)
				}
			},
			initload(){
				let _that = this
				let arr = [
					_that.om.request(
						"productShow/getSource",
						{
							showId:5
						}
					),
					_that.om.request(
						"dictSource/loadCombo",
						{
							sourceId:289,
							categoryId:6210
						}
					)
				]
				Promise.all(arr).then(res=>{
					_that.$refs.autoform.changeEditor("productList","sourcePropertyId","data",configSelectData(res[0],'id','name'))
					_that.$refs.autoform.initSelectIndex("productList","sourcePropertyId")
					_that.$refs.autoform.changeEditor("productList","productBorrowTypeId","data",configSelectData(res[1],'id','text'))
					_that.$refs.autoform.initSelectIndex("productList","productBorrowTypeId")
				})
			}
		},
		onLoad(e) {
			let _that = this
			subOnLoad(_that, e, function() {
				_that.exists = _that.datas.filter((e,i)=>{
					return (i != _that.index)
				}).map(e=>e.newProductId).join(",")
				if (e.configDataCacheKey) {
					uni.getStorage({
						key: e.configDataCacheKey,
						success: function(res) {
							//if (res.data && res.data instanceof Array) {
							if(res.data && Object.prototype.toString.call(res.data)==='[object Array]'){
								_that.autoformdata = res.data
							}
							Object.assign(_that.initValue, _that.formdata)
							_that.autoformdata[0].order = _that.order
							changeEditor("productList","newProductId",{
									link: '/pages/public/pageSearch/pageSearch',
									linkParam: {
										other: JSON.stringify({
											salePropertyIds:e.salePropertyIds?e.salePropertyIds:'5985',
											ids: _that.exists,
											likeFlag: false
										}),
										namefield:'newProductCode',
										notefield:"newProductName",
										extrafield:"newProductStd",
										searchfield: 'q',
										initload: true,
										url: 'product/getFormList',
										cachename: _that.customerUserCacheKey,
									}
							},null,_that.autoformdata)
							
							changeEditor('productList', 'productUserId', {
								link: "../../public/pageSearch/pageSearch",
								"linkParam": {
									other: JSON.stringify({
										firstUserId: ''
									}),
									searchfield: 'q',
									namefield: "userPosInstName",
									url: "/temployee/getStaffCombogrid",
									initload:true,
									cachename: _that.productUserCacheKey,
									placeholder: "请输入姓名"
								}
							}, null, _that.autoformdata)
							changeEditor(_that.autoformdata[0].editors, "fileList", "fileUploadKey", "96")
							if(_that.formdata.fileList){
								var tempFileList = []
								var fileUrlList = _that.formdata.fileList.map(t=>{
									tempFileList.push(t.fileSaveName)
									return t.fileSaveName
								})
								changeEditor(_that.autoformdata[0].editors, "fileList", "fileUrlList", fileUrlList)
								changeEditor(_that.autoformdata[0].editors, "fileList", "tempFileList", tempFileList)
							}	
							_that.initload()
						}
					})
				}
			})
		},
		onShow() {
			let _that = this
			if (_that.$refs.autoform) {
				uni.getStorage({
					key:_that.customerUserCacheKey,
					success:(res)=>{
						if(res.data){
							let d = res.data
							let formdata = _that.$refs.autoform.formdata
							formdata.newProductId = d.id
							formdata.newProductCode = d.newProductCode
							formdata.newProductName = d.newProductName
							formdata.newProductStd = d.newProductStd
							formdata.englishName = d.englishName
							formdata.newProductCoding = d.newProductCoding
							formdata.productCodes = d.productCodes
							formdata.productName = d.productName
							formdata.productModel = d.productModel
							formdata.salesAttributeCategoryId1 = d.salesAttributeCategoryId1
							formdata.salesAttributeCategoryName1 = d.salesAttributeCategoryName1
							formdata.productCategoryId = d.productCategoryId
							formdata.productCategoryName = d.productCategoryName
							formdata.marketingPlanningProductId = d.marketingPlanningProductId
							formdata.marketingPlanningProductName = d.marketingPlanningProductName
							formdata.productFeature = d.productFeature
							formdata.additionalAttrModel = d.additionalAttrModel
							formdata.additionalAttrFeature = d.additionalAttrFeature
							formdata.projectNeed = d.projectNeed
							formdata.projectFeature = d.projectFeature
							formdata.unitCode = d.unitCode
							formdata.unitName = d.unitName
							formdata.uomGroupCode = d.uomGroupCode
							formdata.uomGroupName = d.uomGroupName
							formdata.uomGroupCategory = d.uomGroupCategory
							//erp
							formdata.productCodes = d.projectCode
							formdata.productName = d.projectName
							formdata.productModel = d.projectStd
							//税率
							formdata.taxRate = d.tax
							if(!formdata.taxRate){
								formdata.taxRate = 13
							}
							//无税销售指导价
							formdata.productMoneyOne = d.adjustedSalePrice
							//折扣
							formdata.contractReduceRate = d.adjustedDiscount
							//销售属性类型
							formdata.salesAttributeCategoryId = d.saleProperty
							formdata.salesGuidancePriceIncludingTax = (Number(formdata.productMoneyOne) * (Number(formdata.taxRate) / 100 + 1)).toFixed(4)
							uni.removeStorage({
								key:_that.customerUserCacheKey
							})
						}
					}
				})
				uni.getStorage({
					key:_that.productUserCacheKey,
					success:(res)=>{
						if(res.data){
							let d = res.data
							let formdata = _that.$refs.autoform.formdata
							formdata.productUserId = d.id
							formdata.productUserName = d.username
							uni.removeStorage({
								key:_that.productUserCacheKey
							})
						}
					}
				})
			}
		}
	}
</script>

<style>
</style>
